﻿#include "tcpform.h"
#include "ui_tcpform.h"

TcpForm::TcpForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TcpForm)
{
    ui->setupUi(this);

    ui->m_serverUrl->setText("127.0.0.1:9001");
    ui->m_clientUrl->setText("127.0.0.1:9001");

    ui->m_serverRecvText->setReadOnly(true);
    ui->m_clientRecvText->setReadOnly(true);

    ui->m_btnStop->setEnabled(false);
    ui->m_btnBreak->setEnabled(false);
    ui->m_btnServerSend->setEnabled(false);
    ui->m_btnClientSend->setEnabled(false);

    connect(ui->m_btnListen, SIGNAL(clicked()), this, SLOT(on_listen()));
    connect(ui->m_btnStop, SIGNAL(clicked()), this, SLOT(on_stop()));
    connect(ui->m_btnServerClear, SIGNAL(clicked()), this, SLOT(on_serverClear()));
    connect(ui->m_btnServerSend, SIGNAL(clicked()), this, SLOT(on_serverSend()));

    connect(ui->m_btnConnect, SIGNAL(clicked()), this, SLOT(on_connect()));
    connect(ui->m_btnBreak, SIGNAL(clicked()), this, SLOT(on_break()));
    connect(ui->m_btnClientClear, SIGNAL(clicked()), this, SLOT(on_clientClear()));
    connect(ui->m_btnClientSend, SIGNAL(clicked()), this, SLOT(on_clientSend()));
}

TcpForm::~TcpForm()
{
    delete ui;
}

void TcpForm::on_listen()
{
    QUrl qUrl = QUrl::fromUserInput(ui->m_serverUrl->text());
    if(!qUrl.isValid())
    {
        return;
    }

    m_pTcpServer = new QTcpServer();
    connect(m_pTcpServer, &QTcpServer::newConnection, this, &TcpForm::on_newConnection);

    if(!m_pTcpServer->listen(QHostAddress(qUrl.host()), static_cast<quint16>(qUrl.port())))//监听
    {
        qDebug() << m_pTcpServer->errorString(); //错误信息
    }

    ui->m_btnListen->setEnabled(false);
    ui->m_btnStop->setEnabled(true);

    emit statusBarShow(QString::fromLocal8Bit("Tcp监听成功"));
}

void TcpForm::on_newConnection()
{
    //获取客户端链接
   auto socket_temp = m_pTcpServer->nextPendingConnection();//根据当前新链接建立一个QTepSocket
   m_pServerTcpSocket = socket_temp;//记录此链接用于后续数据读写
   //链接QTcpSocket的信号槽，以读取新数据
   connect(socket_temp, &QTcpSocket::readyRead, this, &TcpForm::on_serverReceive);
   //当断开链接时发出的信号
   connect(socket_temp, &QTcpSocket::disconnected, this, &TcpForm::on_serverDisconnected);

   ui->m_btnListen->setEnabled(false);

   ui->m_btnServerSend->setEnabled(true);

}

void TcpForm::on_serverReceive()
{
    QByteArray qba = m_pServerTcpSocket->readAll(); //读取
    qDebug() << qba;
    ui->m_serverRecvText->appendPlainText(QString(qba));
}

void TcpForm::on_serverDisconnected()
{
    ui->m_btnServerSend->setEnabled(false);
}

void TcpForm::on_stop()
{
    if(m_pServerTcpSocket != nullptr)
    {
        m_pServerTcpSocket->close();
        delete m_pServerTcpSocket;
        m_pServerTcpSocket = nullptr;
    }

    m_pTcpServer->close();
    delete m_pTcpServer;
    m_pTcpServer = nullptr;

    ui->m_btnListen->setEnabled(true);
    ui->m_btnStop->setEnabled(false);

    ui->m_btnServerSend->setEnabled(false);

    emit statusBarShow(QString::fromLocal8Bit("Tcp服务端断开"));
}

void TcpForm::on_serverClear()
{
    ui->m_serverRecvText->clear();

    emit statusBarShow(QString::fromLocal8Bit("Tcp清空内容"));
}

void TcpForm::on_serverSend()
{
    QString strMesg= ui->m_serverSendText->toPlainText();

    m_pServerTcpSocket->write(strMesg.toUtf8()); //发送
}

void TcpForm::on_connect()
{
    QUrl qUrl = QUrl::fromUserInput(ui->m_clientUrl->text());
    if(!qUrl.isValid())
    {
        return;
    }

    m_pClientTcpSocket = new QTcpSocket(this);
    m_pClientTcpSocket->abort();
    m_pClientTcpSocket->connectToHost(QHostAddress(qUrl.host()), static_cast<quint16>(qUrl.port()));

    connect(m_pClientTcpSocket, SIGNAL(readyRead()), this, SLOT(on_clientRecive()));
    connect(m_pClientTcpSocket, &QTcpSocket::disconnected, this, &TcpForm::on_clientDisconnected);

    ui->m_btnConnect->setEnabled(false);
    ui->m_btnBreak->setEnabled(true);

    ui->m_btnClientSend->setEnabled(true);

    emit statusBarShow(QString::fromLocal8Bit("Tcp连接成功"));
}

void TcpForm::on_break()
{
    m_pClientTcpSocket->close();
    delete m_pClientTcpSocket;
    m_pClientTcpSocket = nullptr;

    ui->m_btnConnect->setEnabled(true);
    ui->m_btnBreak->setEnabled(false);

    ui->m_btnClientSend->setEnabled(false);

    emit statusBarShow(QString::fromLocal8Bit("Tcp客户端断开"));
}

void TcpForm::on_clientClear()
{
    ui->m_clientRecvText->clear();

    emit statusBarShow(QString::fromLocal8Bit("Tcp清空内容"));
}

void TcpForm::on_clientSend()
{
    QString text = ui->m_clientSendText->toPlainText();
    m_pClientTcpSocket->write(text.toUtf8());
}

void TcpForm::on_clientRecive()
{
    QByteArray qba = m_pClientTcpSocket->readAll(); //读取
    qDebug() << qba;
    ui->m_clientRecvText->appendPlainText(QString(qba));
}

void TcpForm::on_clientDisconnected()
{
    ui->m_btnConnect->setEnabled(true);
    ui->m_btnBreak->setEnabled(false);

    ui->m_btnClientSend->setEnabled(false);
}
